//
//  DZReportAlertView.m

#import "DZReportAlertView.h"
#import "zhPopupController.h"
#import "AppDelegate.h"
#import "DZReportBottomView.h"
#import <AVFAudio/AVFAudio.h>
@interface DZReportAlertView()
@property (nonatomic,strong) zhPopupController *reportAlertViewPopUp;
@property (nonatomic, copy) void (^ReportAlertBlock)(void);
@property (nonatomic,strong) DZReportBottomView *reportBottomView;
@property (nonatomic, strong)AVAudioPlayer * audioPlayer;
@end
@implementation DZReportAlertView
-(instancetype)init{
    if(self == [super init]){
        self.backgroundColor = UIColor.clearColor;
        [self makeSubView];
    }
    return self;
}
- (void)makeSubView{
    UIImageView * reportAlertHeadBgImgView = [[UIImageView alloc] init];
    reportAlertHeadBgImgView.image = DZImageNamed(@"dz_ReportHeadBg");
    reportAlertHeadBgImgView.userInteractionEnabled = YES;
    reportAlertHeadBgImgView.layer.cornerRadius = DZRatio(28);
    reportAlertHeadBgImgView.clipsToBounds = YES;
    [self addSubview:reportAlertHeadBgImgView];
    [reportAlertHeadBgImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DZRatio(0)));
        make.right.equalTo(@(DZRatio(0)));
        make.top.equalTo(@(DZRatio(0)));
        make.height.equalTo(@(DZRatio(650)));
    }];
    UIImageView * reportAlertHeadImgView = [[UIImageView alloc] init];
    reportAlertHeadImgView.image = DZImageNamed(@"dz_ReportHeadIcon");
    reportAlertHeadImgView.userInteractionEnabled = YES;
    [self addSubview:reportAlertHeadImgView];
    [reportAlertHeadImgView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(DZRatio(0)));
        make.centerX.equalTo(@(DZRatio(0)));
        make.width.equalTo(@(DZRatio(152)));
        make.height.equalTo(@(DZRatio(152)));
    }];
    UILabel * reportAlertHeadLab = [[UILabel alloc] init];
    reportAlertHeadLab.font = [UIFont systemFontOfSize:DZRatio(44) weight:UIFontWeightBold];
    reportAlertHeadLab.textColor = DZHex(@"#FFFFFF");
    reportAlertHeadLab.text = @"倒计时预警";
    [self addSubview:reportAlertHeadLab];
    [reportAlertHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(@(DZRatio(0)));
        make.top.equalTo(@(DZRatio(148)));
        make.height.equalTo(@(DZRatio(62)));
    }];
    UILabel * reportAlertLab = [[UILabel alloc] init];
    reportAlertLab.font = [UIFont systemFontOfSize:DZRatio(32) weight:UIFontWeightRegular];
    reportAlertLab.textColor = DZHex(@"#FFFFFF");
    reportAlertLab.text = @"为了给你提供实时地震预警服务，请授予本app你的定位和通知权限，用于计算地震预警时间并及时提醒";
    reportAlertLab.textAlignment = NSTextAlignmentCenter;
    reportAlertLab.numberOfLines = 0;
    [self addSubview:reportAlertLab];
    [reportAlertLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DZRatio(32)));
        make.right.equalTo(@(-DZRatio(32)));
        make.top.equalTo(reportAlertHeadLab.mas_bottom).offset(DZRatio(14));
        make.height.equalTo(@(DZRatio(148)));
    }];
    UIButton * reportAlertVoiceBtn = [[UIButton alloc] init];
    [reportAlertVoiceBtn addTarget:self action:@selector(reportAlertVoice) forControlEvents:UIControlEventTouchUpInside];
    [reportAlertVoiceBtn setBackgroundImage:DZImageNamed(@"dz_ReportTestVoice") forState:UIControlStateNormal];
    [self addSubview:reportAlertVoiceBtn];
    [reportAlertVoiceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.width.equalTo(@(DZRatio(464)));
        make.height.equalTo(@(DZRatio(72)));
        make.top.equalTo(reportAlertLab.mas_bottom).offset(DZRatio(40));
    }];
    self.reportBottomView = [[DZReportBottomView alloc] init];
    [reportAlertHeadBgImgView addSubview:self.reportBottomView];
    [self.reportBottomView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DZRatio(0)));
        make.right.equalTo(@(DZRatio(0)));
        make.height.equalTo(@(DZRatio(108)));
        make.bottom.equalTo(@(DZRatio(0)));
    }];
    UIView * reportOpenView = [[UIView alloc] init];
    reportOpenView.backgroundColor = UIColor.whiteColor;
    reportOpenView.layer.cornerRadius = DZRatio(28);
    reportOpenView.clipsToBounds = YES;
    [self addSubview:reportOpenView];
    [reportOpenView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DZRatio(0)));
        make.right.equalTo(@(DZRatio(0)));
        make.top.equalTo(reportAlertHeadBgImgView.mas_bottom).offset(DZRatio(30));
        make.height.equalTo(@(DZRatio(108)));
    }];
    UILabel * reportOpenHeadLab = [[UILabel alloc] init];
    reportOpenHeadLab.font = [UIFont systemFontOfSize:DZRatio(28) weight:UIFontWeightBold];
    reportOpenHeadLab.textColor = DZHex(@"#222222");
    reportOpenHeadLab.text = @"开启实时地震预警";
    [reportOpenView addSubview:reportOpenHeadLab];
    [reportOpenHeadLab mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(DZRatio(29)));
        make.top.equalTo(@(DZRatio(0)));
        make.bottom.equalTo(@(DZRatio(0)));
    }];
    UIButton * reportOpenVoiceBtn = [[UIButton alloc] init];
    [reportOpenVoiceBtn addTarget:self action:@selector(reportOpenVoice:) forControlEvents:UIControlEventTouchUpInside];
    [reportOpenVoiceBtn setBackgroundImage:DZImageNamed(@"dz_ReportClose") forState:UIControlStateNormal];
    [reportOpenVoiceBtn setBackgroundImage:DZImageNamed(@"dz_ReportOpen") forState:UIControlStateSelected];
    [reportOpenView addSubview:reportOpenVoiceBtn];
    [reportOpenVoiceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(reportOpenView);
        make.width.equalTo(@(DZRatio(92)));
        make.height.equalTo(@(DZRatio(56)));
        make.right.equalTo(@(-DZRatio(26)));
    }];
    UIButton * reportCloseVoiceBtn = [[UIButton alloc] init];
    [reportCloseVoiceBtn addTarget:self action:@selector(reportCloseVoice) forControlEvents:UIControlEventTouchUpInside];
    [reportCloseVoiceBtn setBackgroundImage:DZImageNamed(@"dz_ReportAlertClose") forState:UIControlStateNormal];
    [self addSubview:reportCloseVoiceBtn];
    [reportCloseVoiceBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self);
        make.width.equalTo(@(DZRatio(80)));
        make.height.equalTo(@(DZRatio(80)));
        make.bottom.equalTo(@(DZRatio(0)));
    }];
}
- (void)reportAlertVoice{
    if(_audioPlayer.isPlaying){
        [_audioPlayer stop];
    }else{
        DZWeakSelf
//        NSString *audioPath = [[NSBundle mainBundle] pathForResource:@"resouce" ofType:@"m4a"];
        NSURL *audioUrl = [NSURL URLWithString:@"http://tianxingdizhen.3o5y1b.top/dizhen/static/resouce.m4a"];
        dispatch_async(dispatch_get_main_queue(), ^{
            NSData *audioData = [NSData dataWithContentsOfURL:audioUrl];
            weakSelf.audioPlayer = [[AVAudioPlayer alloc] initWithData:audioData error:nil];
            [weakSelf.audioPlayer play];
        });
    }
}
- (void)reportOpenVoice:(UIButton *)sender{
    DZWeakSelf
    [DzAuthorizationHelper requestLocationAuthorization:^(DzAuthorizationStatus status) {
        if(status == DzAuthorizationAuthorized){
            sender.selected = !sender.isSelected;
            if(weakSelf.ReportAlertBlock){
                weakSelf.ReportAlertBlock();
                [weakSelf.reportAlertViewPopUp dismiss];
            }
        }else{
            UIAlertController * alert = [UIAlertController alertControllerWithTitle:@"您还没有允许定位权限" message:@"去设置一下吧" preferredStyle:UIAlertControllerStyleAlert];
            UIAlertAction * cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {

            }];
            UIAlertAction * setAction = [UIAlertAction actionWithTitle:@"去设置" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
                dispatch_async(dispatch_get_main_queue(), ^{
                    NSURL * url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                    [UIApplication.sharedApplication openURL:url options:@{} completionHandler:nil];
                });
            }];
            [alert addAction:cancelAction];
            [alert addAction:setAction];
            [((AppDelegate*)([UIApplication sharedApplication].delegate)).window.rootViewController presentViewController:alert animated:YES completion:nil];
        }
    }];
    
}
- (void)reportCloseVoice{
    [_reportAlertViewPopUp dismiss];
}
+ (void)showReportAlertView:(void(^)(void))complaint{
    DZReportAlertView *reportAlertView = [[DZReportAlertView alloc] init];
    zhPopupController *reportAlertViewPopUp = [[zhPopupController alloc] initWithView:reportAlertView size:CGSizeMake(DZRatio(652), DZRatio(910))];
    reportAlertView.ReportAlertBlock = complaint;
    reportAlertView.reportAlertViewPopUp = reportAlertViewPopUp;
    reportAlertViewPopUp.presentationStyle = zhPopupSlideStyleTransform;
    reportAlertViewPopUp.layoutType = zhPopupLayoutTypeCenter;
    reportAlertViewPopUp.presentationTransformScale = 0.85;
    reportAlertViewPopUp.dismissonTransformScale = 0.85;
    reportAlertViewPopUp.maskAlpha = 0.6;
    reportAlertViewPopUp.dismissOnMaskTouched = YES;
    [reportAlertViewPopUp showInView:((AppDelegate*)([UIApplication sharedApplication].delegate)).window completion:^{
      
    }];
}
@end
